import { createRouter, createWebHistory } from 'vue-router'
import i18n from '../i18n'
import defineRoutes from './routes/defineRoutes'

const router = createRouter({
  history: createWebHistory(), 
  routes: defineRoutes
})

// 全局路由守卫:处理lang语言参数
router.beforeEach((to, from, next) => {
  const lang = to.query.lang
  if (lang && ['en', 'zh-CN'].includes(lang)) {
    i18n.global.locale.value = lang
  } else {
    const defaultLang = i18n.global.locale.value || 'en'//如果i18n.global.locale.value为空，则使用en
    next({
      name: to.name,
      query: { ...to.query, lang: defaultLang }
    })
    return
  }
  next()
})

export default router